(function(){

function getCSSLint(){
	if (typeof(CSSLint) != "undefined")
		return CSSLint;
	
	var fso = new ActiveXObject("Scripting.FileSystemObject");
	
	var fn = Editor.nppDir+"\\Plugins\\jN\\includes\\csslint\\csslint.js";
	if (fso.FileExists(fn)){
		var f = fso.OpenTextFile(fn,1, false,0);
		var scr = decodeFrom(65001,f.ReadAll());
		if (scr && scr.length>0){
			try{
				addScript(scr);
			}catch(e){
				alert(e.message);
			}
		}
		f.Close();
	}else{
		alert("csslint not found");
	}
	
	return CSSLint;
}

var settings = GlobalSettings.get("CSSLint") || {};

var CSSLintMenuCfg = {
	text:"CSS Lint",
	oninitpopup:function(){
		this.verifyMenuItem.disabled  = !/\.css/i.test(currentView.files[currentView.file]);
	}
	
};

var CSSLintMenu = Editor.addMenu(CSSLintMenuCfg);


CSSLintMenuCfg.verifyMenuItem = CSSLintMenu.addItem({
	text:"Verify",
	cmd:function(){
		var csslint = getCSSLint();
		var rules = csslint.getRules();
		var allowed = {};
		for(var i=0; i<rules.length; i++){
			if (settings[rules[i].id] == 1)
				continue;
						
			allowed[rules[i].id] = 1;
		}
		
	    var results = csslint.verify(currentView.text, allowed);
		var messages = [];
		for(var i=0; i<results.messages.length; i++)
			messages.push(this.getCells(results.messages[i]));
			
		this.showDialog(messages);	
	},
			
	getCells:function(m){
		return ["<i class='"+m.type+"'/>",m.line,m.col,m.rule.name,m.message];
	},
	css: "i.warning{ background-position:-16px 0;} i.warning,i.error{width:16px; height:16px; background-repeat: no-repeat; background-image: URL()}",
	header: ["","Line", "Col", "Title","Description"],
	dockable:{name:"CSSLint", docking:"bottom"},
	
	showDialog:function(messages){
		var dialCfg = new Dialog.Grid({
			view: Editor.currentView,

			dockable:this.dockable,
			rows:messages,
			title:"Results",
			header: this.header,
			css: this.css,
			onRowClick:function(cell, target){
				var row = cell.parentNode;
				var line = row.cells[1].innerText;
				var col  = row.cells[2].innerText;
				
				var lnNr = 1*line -1;
				
				// scroll to the line
				this.view.line = lnNr;
				
				//set current column
				var lineObj = this.view.lines.get(lnNr);
				
				var posNr = lineObj.start + col*1 -1;
				this.view.pos = posNr;
				this.view.anchor = posNr;
				
				// aktivate view
				MenuCmds.VIEW_SWITCHTO_OTHER_VIEW();
				if (this.view != Editor.currentView)
					MenuCmds.VIEW_SWITCHTO_OTHER_VIEW();
			}
		});
		
		new Dialog(dialCfg);
	}
})

CSSLintMenu.addSeparator();


CSSLintMenu.addItem({
	text:"Settings",
	cmd:function(){
	    var rules = getCSSLint().getRules();
		var rows = [];
		for(var i=0,c=rules.length; i<c; i++){
			rows.push(this.getCells(rules[i]));
		}

		this.showDialog(rows);	
	},
	getCells:function(rule){
		var checked = settings[rule.id] != 1?"checked=''":""; // negative logic, if exists then not selected
		return ["<input type='checkbox' "+checked+" name='"+rule.id+"'>",rule.name,rule.desc];
	},

	showDialog:function(rules){
		var dialCfg = new Dialog.Grid({
			title : "CSSLint - Settings",
			rows  : rules,
			header: ["","Title","Description"],
			clientHeight: 200,
			clientWidth: 400,
			onRowClick:function(cell, target){
				var row = cell.parentNode;

				if (target.tagName != "INPUT"){
					target = row.cells[0].firstChild;
					this.toggle(target);
				}
				
				if (!target.checked)
					settings[target.getAttribute("name")] = 1;
				else
					delete settings[target.getAttribute("name")];
				
				GlobalSettings.set("CSSLint",settings);
			},
			toggle:function(checkbox){
				checkbox.checked = !checkbox.checked;
			}
		});
		
		new Dialog(dialCfg);
	}
})

CSSLintMenu.addSeparator();

CSSLintMenu.addItem({
	text:"Visit CSS Lint",
	cmd: function(){
		var shell = new ActiveXObject("WScript.Shell");
		if (shell){
			shell.run("http://csslint.net");
		}
	}
});

})();